package com.swancloud.openstack.networking.internal.ext.fwaas;

import java.util.List;
import java.util.Map;

import com.swancloud.api.networking.v20210120.fwaas.FirewallService;
import com.swancloud.core.transport.ExecutionOptions;
import com.swancloud.core.transport.propagation.PropagateOnStatus;
import com.swancloud.model.common.ActionResponse;
import com.swancloud.model.network.v20210120.fwaas.Firewall;
import com.swancloud.model.network.v20210120.fwaas.FirewallUpdate;
import com.swancloud.openstack.compute.functions.ToActionResponseFunction;
import com.swancloud.openstack.networking.v20210120.domain.ext.fwaas.NeutronFirewall;
import com.swancloud.openstack.networking.v20210120.domain.ext.fwaas.NeutronFirewall.Firewalls;
import com.swancloud.openstack.networking.internal.BaseNetworkingServices;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Networking (Neutron) FwaaS Firewall Extension API
 *
 * @author Vishvesh Deshmukh
 */
public class FirewallServiceImpl extends BaseNetworkingServices implements FirewallService {

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<? extends Firewall> list() {
		return get(Firewalls.class, uri("/fwaas/firewall_groups")).execute().getList();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<? extends Firewall> list(Map<String, String> filteringParams) {
		Invocation<Firewalls> req = get(Firewalls.class, uri("/fwaas/firewall_groups"));
		if (filteringParams != null) {
            for (Map.Entry<String, String> entry : filteringParams.entrySet()) {
            	req = req.param(entry.getKey(), entry.getValue());
            }
        }
		return req.execute().getList();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Firewall get(String firewallId) {
		checkNotNull(firewallId);
		return get(NeutronFirewall.class, uri("/fwaas/firewall_groups/%s", firewallId)).execute();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ActionResponse delete(String firewallId) {
		checkNotNull(firewallId);
		return ToActionResponseFunction.INSTANCE.apply(delete(Void.class,
				uri("/fwaas/firewall_groups/%s", firewallId)).executeWithResponse());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Firewall create(Firewall firewall) {
		return post(NeutronFirewall.class, uri("/fwaas/firewall_groups")).entity(firewall)
				.execute(ExecutionOptions.<NeutronFirewall>create(PropagateOnStatus.on(404)));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Firewall update(String firewallId, FirewallUpdate firewallUpdate) {
		checkNotNull(firewallId);
        checkNotNull(firewallUpdate);
        return put(NeutronFirewall.class, uri("/fwaas/firewall_groups/%s", firewallId)).entity(firewallUpdate).execute();
	}

}
